import Vue from "vue";
import VueRouter from "vue-router";
import Vant, { Toast } from "vant";

Vue.use(Vant);

Vue.use(VueRouter);

const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: "/",
    name: "MSite",
    component: (resolve) => require(["../views/MSite/MSite.vue"], resolve),
    meta: {
      showFooter: true,
    },
  },
  {
    path: "/equipment",
    name: "Equipment",
    component: (resolve) => require(["../views/Order/Equipment.vue"], resolve),
    meta: {
      showFooter: true,
    },
  },
  {
    path: "/profile",
    name: "Profile",
    component: (resolve) => require(["../views/Profile/Profile.vue"], resolve),
    meta: {
      showFooter: true,
    },
  },
  {
    path: "/homeland",
    name: "HomeLand",
    component: (resolve) => require(["../views/Dynamic/HomeLand.vue"], resolve),
    meta: {
      showFooter: true,
    },
  },
  {
    path: "/physical",
    name: "physical",
    component: (resolve) =>
      require(["../views/registration/physical.vue"], resolve),
    meta: {
      showFooter: true,
    },
    beforeEnter: (to, from, next) => {
      let IdCard = localStorage.getItem("IdCard");
      if (IdCard) {
        next();
      } else {
        if (to.path !== "/autonym") {
          Toast("请先实名认证");
          next({ path: "/autonym" });
        } else {
          next();
        }
      }
    },
  },
  {
    path: "/register",
    nname: "Register",
    component: (resolve) => require(["../views/Login/Register.vue"], resolve),
  },
  {
    path: "/login",
    name: "Login",
    component: (resolve) => require(["../views/Login/Login.vue"], resolve),
  },
  {
    path: "/registration",
    name: "registration",
    component: (resolve) =>
      require(["../views/registration/registration.vue"], resolve),
  },
  {
    path: "/community",
    name: "community",
    component: (resolve) =>
      require(["../views/registration/community.vue"], resolve),
  },
  {
    path: "/health",
    name: "health",
    component: (resolve) =>
      require(["../views/registration/health.vue"], resolve),
  },
  {
    path: "/linechart",
    name: "lineChart",
    component: (resolve) =>
      require(["../views/registration/lineChart.vue"], resolve),
  },
  {
    path: "/information",
    name: "information",
    component: (resolve) =>
      require(["../views/Profile/information.vue"], resolve),
    meta: {
      keepAlive: true, //不需要被缓存的组件
      isBack: false,
    },
  },
  {
    path: "/phone",
    name: "phone",
    component: (resolve) =>
      require(["../views/registration/phone.vue"], resolve),
  },
  {
    path: "/hospital",
    name: "hospital",
    component: (resolve) => require(["../views/Dynamic/hospital.vue"], resolve),
  },
  {
    path: "/set",
    name: "set",
    component: (resolve) => require(["../views/Login/set.vue"], resolve),
  },
  {
    path: "/safety",
    name: "safety",
    component: (resolve) => require(["../views/Login/safety.vue"], resolve),
  },
  {
    path: "/userItem",
    name: "userItem",
    component: (resolve) => require(["../views/Login/userItem.vue"], resolve),
  },
  {
    path: "/autonym",
    name: "autonym",
    component: (resolve) => require(["../views/Profile/autonym.vue"], resolve),
  },
  {
    path: "/shopping",
    name: "shopping",
    component: (resolve) => require(["../views/Profile/shopping.vue"], resolve),
  },
  {
    path: "/wallet",
    name: "wallet",
    component: (resolve) => require(["../views/Profile/wallet.vue"], resolve),
  },
  {
    path: "/map",
    name: "map",
    component: (resolve) => require(["../views/registration/map.vue"], resolve),
  },
  {
    path: "/tuijian/:id",
    name: "tuijian",
    component: (resolve) =>
      require(["../views/registration/tuijian.vue"], resolve),
  },
  {
    path: "/IsRegist",
    name: "IsRegist",
    component: (resolve) =>
      require(["../views/registration/IsRegist.vue"], resolve),
  },
  {
    path: "/yang/:id",
    name: "yang",
    component: (resolve) =>
      require(["../views/registration/yang.vue"], resolve),
  },
  {
    path: "/dongtai",
    name: "dongtai",
    component: (resolve) => require(["../views/Dynamic/dongtai.vue"], resolve),
  },
];

const router = new VueRouter({
  routes,
});
//全局前置守卫
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token"); //验证登录状态
  if (token) {
    //判断是否登录
    next();
  } else {
    if (to.path !== "/login" && to.path !== "/register") {
      console.log(this);
      Toast("请先登录");
      next({ path: "/login" });
    } else {
      next();
    }
  }
});

export default router;
